<?php
namespace app\common\model;

use think\db\Query;

class Model extends \think\Model
{
    const BOOLEAN_TEXT = [
        0 => '否',
        1 => '是',
    ];
    // 用户等级
    const LEVEL_TEXT = [
        1 => '体验会员',
        2 => '高级会员',
    ];

    //订单类型
    const ORDER_TEXT = [
        1 => '正式订单',
        2 => '体验订单',
    ];

    //用户体验收益资格
    const EXPERIENCE_TEXT = [
       0 => '关闭', 1 => '开启',
    ];

    //是否为软删除
    public $softDelete = true;

    //软删除字段默认值
    protected $defaultSoftDelete = 0;

    //可搜索字段
    protected $searchField = [];

    //可作为条件的字段
    protected $whereField = [];

    //禁止删除的数据id
    public $noDeleteId = [];

    /**
     * 查询处理
     * @var Query $query
     * @var array $param
     */
    public function scopeWhere(Query $query, array $param)
    {
        //关键词like搜索
        $keywords = $param['_keywords'] ?? '';
        if (!empty($keywords) && count($this->searchField) > 0) {
            $this->searchField = implode('|', $this->searchField);
            $query->where($this->searchField, 'like', '%' . $keywords . '%');
        }

        //字段条件查询
        if (count($this->whereField) > 0 && count($param) > 0) {
            foreach ($param as $key => $value) {
                if (!empty($value) && in_array($key, $this->whereField)) {
                    $query->where($key, $value);
                }
            }
        }

        //排序
        $order = $param['_order'] ?? '';
        $by    = $param['_by'] ?? 'desc';
        $query->order($order ?: 'id', $by ?: 'desc');
    }


}